import axios, { AxiosInstance, AxiosResponse } from 'axios';

const request = axios.create({
  baseURL: 'http://192.168.3.133:9989'
});

// 添加请求拦截器
request.interceptors.request.use(function (config) {

  let token: string | null = localStorage.getItem('TOKEN');
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token;
  }

  return config;
}, function (error) {
  return Promise.reject(error);
});


// 添加响应拦截器
request.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  return Promise.reject(error);
});

const http = {
  get<T>(url: string, params?: any, config?: {}): Promise<T> {
    return new Promise((resolve, reject) => {
      request
        .get<T>(url, { params, ...config })
        .then((res: AxiosResponse<T>) => {
          resolve(res.data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  post<T>(url: string, data?: any, config?: {}): Promise<T> {
    return new Promise((resolve, reject) => {
      request
        .post<T>(url, data, config)
        .then((res: AxiosResponse<T>) => {
          resolve(res.data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  put<T>(url: string, data?: any, config?: {}): Promise<T> {
    return new Promise((resolve, reject) => {
      request
        .put<T>(url, data, config)
        .then((res: AxiosResponse<T>) => {
          resolve(res.data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  delete<T>(url: string, config?: {}): Promise<T> {

    return new Promise((resolve, reject) => {
      request
        .delete<T>(url, config)
        .then((res: AxiosResponse<T>) => {
          resolve(res.data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};

export default http;